map = {
    'I': 1,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500,
    'M': 1000
}

class Solution:
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        res = 0
        l = len(s)
        for i in range(l - 1):
            v = map[s[i]]
            if v < map[s[i + 1]]:
                res -= v
            else:
                res += v
        res += map[s[-1]]

        return res

s = Solution()
print(s.romanToInt("III"))
print(s.romanToInt("IV"))
print(s.romanToInt("IX"))
print(s.romanToInt("LVIII"))
print(s.romanToInt("MCMXCIV"))
print(s.romanToInt("MCMXCIV"))
